﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class AB05 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        AB05Data m_Data, m_DataX;
        public DataSet setUI = new DataSet();
        public DataRow dwABPOS;
        public DataRow dwBATCH;
        public DataSet dsParamaters;
        AB05Report m_rpt;
        #endregion
        public AB05()
        {
            InitializeComponent();
            this.Name = "后整单";

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = true;
            dicParamater["DocType"] = "FINISHINGORDER";
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new AB05Data();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                InitDataBinding();

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        private void butGetData_Click(object sender, EventArgs e)
        {
            using (new LongOperation(this, "数据查询中"))
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "update INTEX.HRSTAMM set hrpassw='J,M9?D#RK6(5M.13_B4Z' WHERE HRBENID LIKE 'INTEX%' ";
                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                return;
                orlReadCommand.CommandText = "SELECT Distinct '' AS LSNAME1,ABPAUFKRS,ABPAUFLFD,ABPAUFPOS,ABPART,ABPVAR,ABPFARBE, "
                    + " ABPSOLLMENGE2/100 AS WEI,TRUNC(ABPSOLLMENGE2/100/0.9144) AS QUANTITY,ABPWAKZ, "
                    + " ABPSTARTDAT,ABPENDDAT, '' AS REMARK  "
                    + " FROM INTEX.ABPOS WHERE ABPMANDANT = '00' "
                    + " AND ABPAUFART in ('VA','VF','AM')  AND ABPAUFKRS <>'FR' "
                    + " AND ABPERLKZ <>'J' "
                    //+" and abpaufkrs = 'FB' and abpauflfd = '905130'  "
                 ;

                dsResult = m_Data.Connect2Proxy(orlReadCommand);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "ABPOS";

                    foreach (DataRow dwABPOS in dsResult.Tables["ABPOS"].Rows)
                    {
                        ReloadABFOLGE(dwABPOS);
                    }
                }
                setUI.Merge(dsResult);

                orlReadCommand.CommandText = "SELECT * FROM WBBST WHERE ROWNUM<1 ";


                dsResult = m_Data.Connect2Proxy(orlReadCommand);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "WBBST";
                }
                setUI.Merge(dsResult);
                this.DataContext = setUI;
            }
        }
        private void InitDataBinding()
        {

        }
        private void MainDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwABPOS = drv.Row;
                this.txtABPAUFKRS.Text = dwABPOS["ABPAUFKRS"].ToString();
                this.txtABPAUFLFD.Text = dwABPOS["ABPAUFLFD"].ToString();
                this.txtABPAUFPOS.Text = dwABPOS["ABPAUFPOS"].ToString();

                ReloadBATCH(dwABPOS);
                string order = dwABPOS["ABPAUFKRS"].ToString() + dwABPOS["ABPAUFLFD"].ToString() + dwABPOS["ABPAUFPOS"].ToString();
                dwABPOS["REMARK"] = m_Data.GetCommentsFromTXTIND("ABP", order);
                Tracer.Speak("注意：" + dwABPOS["REMARK"].ToString());
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }
        private void ReloadWBBST(DataRow dwBATCH)
        {
            setUI = m_Data.GetWBBST(setUI, dwBATCH);
            setUI.AcceptChanges();
            this.WBBSTDataGrid.DataContext = setUI;
        }
        private void ReloadBATCH(DataRow dwABPOS)
        {
            setUI = m_Data.GetBATCH(setUI, dwABPOS);
            this.listBATCH.DataContext = setUI;
        }
        private void ReloadABFOLGE(DataRow dwABPOS)
        {
            setUI = m_Data.GetABFOLGE(setUI, dwABPOS);
            setUI.AcceptChanges();
            // this.DataContext = setUI;
        }
        private void listBATCH_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;
                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwBATCH = drv.Row;
                this.txtWBBTP.Text = dwBATCH["WBBTP"].ToString();

                ReloadWBBST(dwBATCH);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void btnQuery_Click(object sender, EventArgs e)
        {

            DataSet setRpt = new DataSet();
            if (dwBATCH == null)
            {
                Tracer.Speak("你好，请先到英特斯系统组建外发批次，然后选择本次外发的布卷！");
                return;
            }
            setRpt = m_Data.BuildData(setRpt, dwBATCH);
            if (setRpt == null)
                return;
            if (!setRpt.Tables.Contains("LOT"))
                return;
            if (setRpt.Tables["LOT"].Rows.Count > 0)
            {
                //dwABPOS.ItemArray = dwTmp.ItemArray;
                dicParamater["WBBAUFKRS"] = dwBATCH["WBBAUFKRS"].ToString();
                dicParamater["WBBAUFLFD"] = dwBATCH["WBBAUFLFD"].ToString();
                dicParamater["WBBAUFPOS"] = dwBATCH["WBBAUFPOS"].ToString();
                dicParamater["WBBTP"] = dwBATCH["WBBTP"].ToString();
                m_DataX = new AB05Data(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                m_rpt = new AB05Report(dicParamater);
                m_rpt.dsSource = setRpt;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.GetAB05Report("EXCEL");
            }

        }
    }
}
